package com.xliu.cs.openproject.lianliankan.music;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 音乐播放控制器
 */
public class MusicPlayer {
    private static final Logger LOG = LoggerFactory.getLogger(MusicPlayer.class);

    private List<File> playLists;

    /** current playing music, using by different thread. */
    private volatile Music music;
    private volatile boolean changePlayLists;

    public static final MusicPlayer instance = new MusicPlayer();

    private final ExecutorService musicPlayExecutor = Executors.newSingleThreadExecutor();

    private MusicPlayer() {
    }

    public static MusicPlayer getInstance() {
        return instance;
    }

    public void play(File file) {
        if (!file.exists()) {
            System.err.println("File Not Exist");
        }
        playLists = new ArrayList<>();
        // only support mp3 format
        if (file.isFile() && file.getName().endsWith(".mp3")) {
            playLists.add(file);
        } else if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (!f.isDirectory() && f.getName().endsWith(".mp3")) {
                        playLists.add(f);
                    }
                }
            }
        }
        if (playLists == null || playLists.isEmpty()) {
            System.out.println("MusicPlayer: no mp3 music found");
        }

        // submit the task
        musicPlayExecutor.submit(() -> {
            changePlayLists = false;
            while (!changePlayLists) {
                // play the playlists
                for (File f : playLists) {
                    if (changePlayLists) {
                        break;
                    }
                    music = new Music(f);
                    music.play();
                }
            }
            LOG.info("change the playlist, will re-play.");
        });
    }

    // 设置音乐播放路径
    public void setMusicPathAndReplay(File file) {
        System.out.println("After Kill : " + Thread.activeCount());

        // stop current music playing
        changePlayLists = true;
        stopCurrentMusic();

        System.out.println("After Kill : " + Thread.activeCount());

        play(file);
    }

    public void stopCurrentMusic() {
        if (music != null) {
            music.stop();
        }
    }

    public void suspendCurrentMusic() {
        if (music != null) {
            music.suspend();
        }
    }

    public void resumeCurrentMusic() {
        if (music != null) {
            music.resume();
        }
    }

    public static void main(String[] args) throws Exception {
        MusicPlayer pm = MusicPlayer.getInstance();
        pm.play(new File("./res/music/test.mp3"));
        System.out.println(Thread.activeCount());
        System.out.println("Main Over");
    }

}
